package com.yl.zqy.service.impl.sys;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yl.zqy.common.MemoryCache;
import com.yl.zqy.common.Result;
import com.yl.zqy.entity.sys.SysDictData;
import com.yl.zqy.mapper.sys.SysDictDataMapper;
import com.yl.zqy.service.sys.SysDictDataService;
import com.yl.zqy.utils.QueryUtil;
import com.yl.zqy.vo.common.IPageVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.yl.zqy.constant.ZQYConstant.CACHE_DICT_DATA;

/**
 * <p>
 * 字典数据 服务实现类
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-25
 */
@Service
@Slf4j
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataMapper, SysDictData> implements SysDictDataService {
    private Map cache = MemoryCache.getInstance().getMemoryCacheMap();

    @Override
    public Result<Map<String, List<SysDictData>>> getDictInfo(List<String> codes) {
        Map<String, List<SysDictData>> map = (Map<String, List<SysDictData>>) MemoryCache.getInstance().getMemoryCacheMap().get(CACHE_DICT_DATA);
        if (!CollectionUtils.isEmpty(codes)) {
            Map<String, List<SysDictData>> result = new HashMap<>();
            codes.stream().forEach(item -> {
                if (map.containsKey(item)) {
                    result.put(item, map.get(item));
                }
            });
            return Result.ok(result);
        }
        return Result.ok(map);
    }

    @Override
    public void initDictDataCache() {
        List<SysDictData> dictDataList = baseMapper.selectList(null);
        Map<String, List<SysDictData>> collect = dictDataList.stream().collect(Collectors.groupingBy(item -> item.getDictCode()));
        cache.put(CACHE_DICT_DATA, collect);
        log.info("系统字典，刷新到缓存成功 - {}", MemoryCache.getInstance().getMemoryCacheMap().get(CACHE_DICT_DATA));
    }

    @Override
    public Result<Boolean> refreshDictDataCache() {
        cache.remove(CACHE_DICT_DATA);
        initDictDataCache();
        return Result.ok(true);
    }

    @Override
    public Result<Boolean> insertDictData(SysDictData dictData) {
        int res = baseMapper.insert(dictData);
        if (res > 0)
            return Result.ok(true);
        return Result.error(false, "新增失败！");
    }

    @Override
    public Result<Boolean> updateDictData(SysDictData dictData) {
        int res = baseMapper.updateById(dictData);
        if (res > 0)
            return Result.ok(true);
        return Result.error(false, "修改失败！");
    }

    @Override
    public Result<IPageVo<SysDictData>> dictDataByPage(int current, int size, String dictTypeId, String dictCode, String query) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                .and(StringUtils.isNotBlank(dictTypeId), c -> c.eq(SysDictData::getDictTypeId, dictTypeId))
                .and(StringUtils.isNotBlank(dictCode), c -> c.eq(SysDictData::getDictCode, dictCode))
                .and(StringUtils.isNotBlank(query), c -> c.like(SysDictData::getDictLabel, query)
                        .or()
                        .like(SysDictData::getDictValue, query))
                .orderByAsc(SysDictData::getSort);
        Page<SysDictData> sysDictDataPage = baseMapper.selectPage(Page.of(current, size), queryWrapper);
        return Result.ok(QueryUtil.iPage2IPageVo(sysDictDataPage));
    }

    @Override
    public Long countDictDataById(String dictTypeId) {
        LambdaQueryWrapper<SysDictData> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictData::getDictTypeId, dictTypeId);
        Long selectCount = baseMapper.selectCount(queryWrapper);
        return selectCount;
    }
}
